<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>人员信息</title>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/ext/resources/css/ext-all.css"></link>
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/ext/resources/css/xtheme-slate.css"></link>

<script src="<%=request.getContextPath() %>/ext/adapter/ext/ext-base.js"></script>
<script src="<%=request.getContextPath() %>/ext/ext-all.js"></script>
<script src="<%=request.getContextPath() %>/ext/src/locale/ext-lang-zh_CN.js"></script>		
<script src="<%=request.getContextPath() %>/js/md5.js"></script>	

<script type="text/javascript">
			Ext.onReady(function(){
				var pwd = encode('88888888');
				
				Ext.Ajax.on('requestcomplete',checkUserSessionStatus, this);      
				function checkUserSessionStatus(conn,response,options){      
				    if(response.getResponseHeader('sessionstatus')){      
				    	parent.location.href='/xdj/index.jsp';   
				    }      
				}  
				var tree;
				var thenode;
				//Ext.BLANK_IMAGE_URL='ext/resources/images/default/s.gif';
				//var panel={};
				var treeloader = new Ext.tree.TreeLoader({   // dataUrl : 'tree.jsp'//这里可以不需要指定URL，在加载前事件响应里面设置 	
				});  
				var rootnode = new Ext.tree.AsyncTreeNode({  // 异步加载根节点 
					id : '2',  
					text : '大坝电厂',  
					cls : 'folder' 
				});  
				thenode=rootnode;
				var tree=new Ext.tree.TreePanel({
					el : 'tree',// 将树形添加到一个指定的div中,非常重要！  
					border:false,
					region : 'west',  
					//title : '功能菜单',  
					lines:true, //线段
					width : 300, 
					height:380,
				//	baseCls:'test',
					//html:'<div id="test"><div>',
					split : true, 
					autoHeight : true,  
					frame : true,// 美化界面    
					enableDD : false,// 是否支持拖拽效果  
					containerScroll : true,// 是否支持滚动条  
					rootVisible : true, // 是否隐藏根节点,很多情况下，我们选择隐藏根节点增加美观性  
					animate : true, // 动画效果  
					root : rootnode,
					loader : treeloader ,
					listeners : {
						'beforeload' : function(node) {
							treeloader.dataUrl = "depart_load?parentID=parentID_" +node.id+"_EQ";
						},
						'click' : function(node) {
							thenode = node ;
							 Ext.apply(store.baseParams , {departmentID:thenode.id});//分页加参数
							store.load({
								params:{
									departmentID:thenode.id,
									start:0,
									limit:10
								}
							});
						}
					}
					
				// 树加载  
				});  
				tree.render();  // 渲染树形 
				var cm=new Ext.grid.ColumnModel([
				             				    new Ext.grid.RowNumberer(),  //自动增加序列号
				             				    //sm, //加到每列的前边
				             				    {header:'人员工号',dataIndex:"operatorCode",sortable:true}, //True表示为可在该列上进行排列
				             				    {header:'员工姓名',dataIndex:"userName"},
				             				    {header:'登陆账号',dataIndex:"userAccount"},
				             				    {header:'passWord',dataIndex:"passWord",hidden:true},
				             				    {header:'岗位',dataIndex:"stationName"},
				             				    {header:'专业',dataIndex:"professName"},
				             				    {header:'备注',dataIndex:"remarks"},
				             				    {header:'stationID',dataIndex:"stationID",hidden:true},
				             				    {header:'professID',dataIndex:"professID",hidden:true},
				             				    {header:'userID',dataIndex:"userID",hidden:true}
				             				]);//定义相应的列模型
				             				var store=new Ext.data.JsonStore({
				             				 	url: 'user_load',
				             				 	root: 'result',
				             					// idProperty:'id',
				             					totalProperty:'totalProperty',
				             				    fields: ["operatorCode", "userName","userAccount","passWord","stationName","professName","remarks","stationID","professID","userID"]
				             				});//存储前两项的定义的东西，将其连接起				
				             				var panel = new Ext.grid.GridPanel({
				             					region:'center',
				             					cm:cm,
				            				    //sm:sm,
				            				    store:store,
				            				    height:document.body.clientHeight,  //设置表格的高度 true为自动获取表格的大小
				            				   // renderTo:'grid' , //渲染容器
				            				    enableColumnMove:false,//不可以使表格列移动
				            				    enableColumnResize:false, //不可以改变列的宽度
				            				    stripeRows:false, //True表示为显示行的分隔符
				            				    loadMask:true,  //会有一个Ext.LoadMask的遮罩效果
				            				    trackMouseOver : true,  //True表示为鼠标移动时高亮显示（默认为true）。
				            				    enableDragDrop : true,
				            				    viewConfig:{
				            				      forceFit:true //让没列自动填满表格，根据你设置的宽度按比例分配
				            				    },
				            				    tbar:[{
				            				    	text: "删除",disabled:true,id:'cDeleteBox',
				            				    	icon:'../image/public/user_delete.png',
				            				    	handler:function() {
				            				    		Ext.MessageBox.confirm("提示","您确定要删除这些信息吗?", function(button,text){   
				            				    			if(button=='yes'){
				            				    				var record=panel.getSelectionModel().getSelected();
				            				    				
				            									if(record ==null){
				            			        					alert("请选择要删除的人员信息");
				            			        					Ext.getBody().unmask();
				            			        				}else{
				            			        					Ext.Ajax.request({
				            											url:'user_delete',
				            											success:function(response){
				            												var result = Ext.util.JSON.decode(response.responseText);
				            												if(result.success==true){
				            													store.load({
				            														params:{
				            															departmentID:thenode.id
				            														}
				            													});
				            													
				            												}else{
				            													Ext.Msg.alert("提示","操作失败，请重试。") ;
				            												}
				            											},
				            											params:{userID:record.get("userID")}
				            										});
				            			        				}
				            									
				            				    			}
				            				    			}); 
				            				    	}
				            					},{
				            						text: "修改",disabled:true,id:'cModifyBox',
				            						icon:'../image/public/user_edit.png',
				            						handler:function() {
				            							Ext.MessageBox.confirm("提示","您确定要修改这些信息吗?", function(button,text){   
				            								if(button=='yes'){
				            									Ext.getBody().mask();
				            									var record=panel.getSelectionModel().getSelected();
				            									if(record ==null){
				            			        					alert("请选择要修改的人员信息");
				            			        					Ext.getBody().unmask();
				            			        				}else{
				            			        					showWindown("user_update");
				            						        		userform.getForm().setValues({
				            						        			"user.operatorCode" : record.get("operatorCode"),
				            						        			"user.userName" : record.get("userName"),
				            						        			"user.userAccount": record.get("userAccount") ,
				            						        			"user.remarks": record.get("remarks") ,
				            						        			"user.userID": record.get("userID"),
				            						        			"user.passWord": record.get("passWord"),
				            						        			"user.profess.professID": record.get("professID"),
				            						        			"user.station.stationID": record.get("stationID"),
				            						        			"user.department.departmentID":thenode.id
				            						        		});
				            			        				}
				            					        		
				            								}
				            								}); 	
				            						}
				            						
				            					},{
				            				    	text: '增加',disabled:true,id:'cAddBox',
				            				    	icon:'../image/public/user_add.gif',
				            				        handler:function(){
				            				        	Ext.getBody().mask();
				            				        	
				            				        	showWindown("user_save");
				            				        }
				            					}],
				            				    bbar:new Ext.PagingToolbar({
				            				    	pageSize : 10, //每页显示几条
				            						store : store,
				            						beforePageText:"当前第",   
				            			            afterPageText:"页，共{0}页",   
				            			            //lastText:"尾页",   
				            			           // nextText :"下一页",   
				            			            //prevText :"上一页",   
				            			           // firstText :"首页",                           
				            				        //displayInfo:true, 
				            				        displayMsg:'显示第{0}条数据到{1}条数据,一共有{2}条', 
				            				        emptyMsg:'没有记录'
				            				        
				            				  })
				            				 });
				            				store.load({params:{start:0,limit:10}});//加载数据  注意，一旦使用了分页条， store.load();就必须在构造表格之后进行执行，否则将不起作用，并且将分页条和数据源关联，从而实现与表格共享数据模型
				function showWindown(urlString) {
					userform=new Ext.form.FormPanel({
					frame:true,
					height:250,
					buttonAlign:'top',
					layout:'form',
					items:[
					
					{
						fieldLabel:'人员工号',
						xtype:'textfield',
						allowBlank:false,
						blankText:'工号不能为空',
						name:'user.operatorCode'
					},{
						fieldLabel:'人员名称',
						xtype:'textfield',
						allowBlank:false,
						blankText:'名称不能为空'	,
						name:'user.userName'
					},{
						fieldLabel:'登陆账号',
						xtype:'textfield',
						allowBlank:false,
						blankText:'账号不能为空',
						name:'user.userAccount'
					},{
						xtype:'hidden',
						name:'user.userID'
					},{
						xtype:'hidden',
						name:'user.department.departmentID',
						value:thenode.id 
					},{
						xtype:'hidden',
						name:'user.passWord',
						value:pwd
					},{
						xtype:'combo',
						fieldLabel: '专业',
		                hiddenName:'user.profess.professID',
		                emptyText:'--请选择--',
		                forceSelection: true,
		                listWidth: 150,
		                store: professStore,
		                valueField:'professID',
		                displayField:'professName',
		                typeAhead: true,
		                mode: 'local',
		                triggerAction: 'all',
		                selectOnFocus:true,
		                allowBlank:false
					},{
						xtype:'combo',
						 fieldLabel: '岗位',
			                hiddenName:'user.station.stationID',
			                emptyText:'--请选择--',
			                forceSelection: true,
			                listWidth: 150,
			                store: stationStore,
			                valueField:'stationID',
			                displayField:'stationName',
			                typeAhead: true,
			                mode: 'local',
			                triggerAction: 'all',
			                selectOnFocus:true,
			                allowBlank:false
					},{
						fieldLabel:'备注',
						xtype:'textarea',
						width:200,
						height:50,
						grow:true, //可自动增长
						preventScrollbars:true,  //不显示那个下拉条 必须与grow一起用
						name:'user.remarks'
					}],
					buttons:[{
						text: "确认",
					       handler:function(){ 
					    	   alert("-------") ;
					    	   if ( userform.getForm().isValid() ){
					    	    userform.form.submit({   
					        	waitMsg : '正在提交数据....',   
					            url:urlString,   
					            method:'post', 
						            success:function(userform,action){  
						            	var result = Ext.util.JSON.decode(action.response.responseText);
						            	if(result.success==true) {
						            		if(urlString=="user_save"){
						            			win.close();
						            			store.load({
													params:{
														departmentID:thenode.id,
														start:0,
						            					limit:10
													}
												});
						            			Ext.getBody().unmask();
						            		}else if(urlString=="user_update"){
						            			win.close() ;
						            			store.load({
													params:{
														departmentID:thenode.id,
														start:0,
						            					limit:10
													}
												});
						            			Ext.getBody().unmask();
						            		}
						            		
						            	}else{
						            		Ext.Msg.alert("提示","操作失败，请重试。") ;
						            	}
						            }
						          }); 
						       }
						       }
				     },{
				        text: "取消",
				        handler:function(){ 
				           	win.close() ;
				           	Ext.getBody().unmask(); 	
				        }
				     }]
					});
					var win=new Ext.Window({
							title:'登录',
							 width:400,
							 height:280,
							 items:[userform],
							 xtype:'form',
							 listeners:{"beforedestroy":function(obj)
				                 { 
								 Ext.getBody().unmask();  
				                    // obj.show(); return false; 
				                  }
							}  
					});
					win.show();
				}
				var form = new Ext.form.FormPanel({
					//height:140,
					border:false,
					width:200,
					html:'<p>部门信息树采用三层结构<br>第一层：企业或企业分厂<br>第二层：企业管理部门<br>第三层：子部门</p>',
					frame:true
				});
				
				 new Ext.Viewport({  
				//	layout : 'border', // 使用border布局  
					layout : 'border', 
					border:false,
					//autoHeight:true,
					//margins:'5 5 5 0',
					items:[{
						region:'west',
						border:true,
						width:200,
						layout:'vbox',
						items:[tree,form]
					},panel],
					listeners:{
						'beforerender' : function(cmp) {
							// set role
							var store=new Ext.data.JsonStore({
								url: '../system/role_loadFunc?funcCode=ControlUser',
								autoLoad : true,
								fields: ["functioncode"]
							});
							store.load({
							    callback: function(records, options, success){
							    	var i=0;
							    	for(; i<records.length; i++) {
							    		var id = records[i].get("functioncode");
							    		//alert('d'+Ext.getCmp('56').text);
							    		if(Ext.getCmp(id)!=null) {
							    			Ext.getCmp(id).setDisabled(false);
							    		}
							        }
							    },
							    scope: store
							});
						}
					}
				 }); 
				// alert(thenode.id+"fdsjifd");
				 //store.baseParams.xml = 'xml';
				 var professStore = new Ext.data.JsonStore({
					 url:"profess_load",
					 autoLoad : true,
					 fields:["professID","professName"]
				 });
				
				 var stationStore = new Ext.data.JsonStore({
					 url:"station_load",
					 autoLoad : true,
					 fields:["stationID","stationName"]
				 });
			
				 
			});
			
			
	</script> 
</head>
<body>
<div id="tree" ></div>
<div id="grid"></div>
</body>
</html>